package com.hgdp.medicalexamination.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgdp.medicalexamination.Dto.MaleAndFmaleDto;
import com.hgdp.medicalexamination.entity.Male;
import com.hgdp.medicalexamination.mapper.MaleMapper;
import com.hgdp.medicalexamination.service.FmaleService;
import com.hgdp.medicalexamination.service.MaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @param /婚前医学检查表(男性)信息
 * @author hgdp
 * @date 2020-07-29 15:19:30
 */
@RestController
@AllArgsConstructor
@Api(value = "婚前医学检查表(男性)信息操作接口")
@RequestMapping("/male")
public class MaleController {

    private MaleService maleService;
    private FmaleService fmaleService;
    private MaleMapper maleMapper;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询", notes = "分页查询male 婚前医学检查表(男性和女性)信息")
    @GetMapping("/pagelist")
    public R list(Page page, Male male) {
        return R.ok(maleService.queryPage(page, male));
    }

    /**
     * 信息
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询male 婚前医学检查表(男性和女性)信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        if (id.equals("")) {
            return R.ok(false);
        }
        String substring = id.substring(id.length() - 1);
        if (substring.equals("1")) {
            return R.ok(maleService.getById(id));
        } else {
            return R.ok(fmaleService.getById(id));
        }
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存", notes = "保存 婚前医学检查表(男性和女性)信息")
    @PostMapping("/save")
    public R save(@RequestBody MaleAndFmaleDto maleAndFmaleDto) {
        return R.ok(maleService.insertMale(maleAndFmaleDto));
    }


    /**
     * 修改婚前医学检查表(男性)信息
     *
     * @param maleAndFmaleDto 婚前医学检查表(男性)信息
     * @return R
     */
    @ApiOperation(value = "修改婚前医学检查表(男性和女性)信息", notes = "修改婚前医学检查表(男性和女性)信息")
    @PostMapping("/update")
    public R update(@RequestBody MaleAndFmaleDto maleAndFmaleDto) {
        if (maleAndFmaleDto.getMale() != null) {
            maleService.updateById(maleAndFmaleDto.getMale());
        }
        if (maleAndFmaleDto.getFmale() != null) {
            fmaleService.updateById(maleAndFmaleDto.getFmale());
        }
        return R.ok(true);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "通过id删除婚前医学检查表(男性和女性)信息", notes = "通过id删除婚前医学检查表(男性和女性)信息")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        ArrayList<String> males = new ArrayList<>();
        ArrayList<String> fmales = new ArrayList<>();
        if (ids.length>0){
            for (String id : ids) {
                if (id.substring(id.length()).equals("1")) {
                    fmales.add(id.substring(0, id.length() - 1) + 2);
                    males.add(id);
                } else {
                    males.add(id.substring(0, id.length() - 1) + 1);
                    fmales.add(id);
                }
            }
        }
        maleService.removeByIds(males);
        fmaleService.removeByIds(fmales);
        return R.ok(true);
    }

    /**
     * 按时间查询
     */
    @ApiOperation(value = "按时间查询婚前医学检查表(男性和女性)信息", notes = "按时间查询婚前医学检查表(男性和女性)信息")
    @PostMapping("/selectByTime")
    public R selectByTime(Page page,String startTime,String endTime){
        return R.ok(maleService.selectByTime(page,startTime,endTime));
    }
}